package core.service;

import core.dao.UserDAO;
import core.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author MG
 * @since 13.03.2014
 */
@Service
@Transactional(readOnly = true)
public class UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    @Autowired
    UserDAO userDAO;

    public User findUser(long id) {
        return userDAO.findUser(id);
    }

    public List<User> getAllUsers(){
        return userDAO.getAllUsers();
    }

    @Transactional
    public void saveUser(User user) {
        logger.debug("Saving user with id = {}, username = {}", user.getId(), user.getUsername());
        //save user
    }

    @Transactional
    public void createUser(User user){
        userDAO.createUser(user);
    }

    @Transactional
    public void removeUser(User user){
        long id = user.getId();
        User searchedUser = userDAO.findUser(id);
        userDAO.removeUser(searchedUser);
    }

    @Transactional
    public void updateUser(User user){
        userDAO.updateUser(user);
    }

    @Transactional
    public void changeNameById(long id){
        User user = findUser(id);
        userDAO.changeNameById(user);
    }

    @Transactional
    public void deleteUser(long id){
        User user = findUser(id);
        userDAO.removeUser(user);
    }

    public User findUserByFirstName(String firstName){
        return userDAO.findUserByFirstName(firstName);
    }

    public List getAllNames(){
        return userDAO.getAllNames();
    }

    public List<User> selectUserWhoCreatedTopics(){
        return userDAO.selectUserWhoCreatedTopics();
    }

    public long getUsersCount(){
        return userDAO.getUserCount();
    }

}
